﻿/*----------------------------------------------------------------
//  <copyright file="IdentityExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-25 17:19</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud
{
    /// <summary>
    /// <see cref="IdentityResult"/> 辅助扩展
    /// </summary>
    public static class IdentityExtensions
    {
        #region "扩展方法"
        #region "将 <see cref="IdentityResult"/> 转化为 <see cref="OperationResult"/>"
        /// <summary>
        /// 将 <see cref="IdentityResult"/> 转化为 <see cref="OperationResult"/>
        /// </summary>
        /// <param name="identityResult"></param>
        /// <returns></returns>
        public static OperationResult ToOperationResult(this IdentityResult identityResult)
        {
            return identityResult.Succeeded ? OperationResult.Success() : OperationResult.Error(identityResult.Errors.Select(m => m.Description).ExpandAndToString());
        }
        #endregion
        #region "将 <see cref="IdentityResult"/> 转化为 <see cref="OperationResult{TUser}"/>"
        /// <summary>
        /// 将 <see cref="IdentityResult"/> 转化为 <see cref="OperationResult{TUser}"/>
        /// </summary>
        /// <typeparam name="TUser"></typeparam>
        /// <param name="identityResult"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static OperationResult<TUser> ToOperationResult<TUser>(this IdentityResult identityResult, TUser user)
        {
            return identityResult.Succeeded ? OperationResult.Success(user) : OperationResult.Error<TUser>(identityResult.Errors.Select(m => m.Description).ExpandAndToString());
        }
        #endregion
        #region "快速创建错误信息的IdentityResult"
        /// <summary>
        /// 快速创建错误信息的IdentityResult
        /// </summary>
        /// <param name="identityResult"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static IdentityResult Failed(this IdentityResult identityResult, params string[] errors)
        {
            var identityErrors = identityResult.Errors;
            identityErrors = identityErrors.Union(errors.Select(m => new IdentityError() { Description = m }));
            return IdentityResult.Failed(identityErrors.ToArray());
        }
        #endregion
        #region "获取IdentityResult的错误信息"
        /// <summary>
        /// 获取IdentityResult的错误信息
        /// </summary>
        /// <param name="identityResult"></param>
        /// <returns></returns>
        public static string ErrorMessage(this IdentityResult identityResult)
        {
            if (identityResult.Succeeded)
            {
                return null;
            }

            return identityResult.Errors.Select(m => m.Description).ExpandAndToString("; ");
        }
        #endregion

        #endregion

    }

}
